<?php
	define('DEBUG', 0);
	if(!DEBUG)
		header('Content-type: text/vnd.wap.wml');
?>

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<card id='main' title="Xem ghi chú">

<p>
<?php
	if ($db = new SQLite3('webnote.db3')) 
	{
		$results = $db->query("SELECT noteid, strftime('%d/%m %H:%M', timestamp) AS timestamp, title, content FROM notes");
		if($results === false)
			echo("Couldn't query table: ".$db->lastErrorMsg());
		else	// there's result:
		{
			//duyệt các phần tử mảng trả về từ csdl
			$i=1;
			while ($row = $results->fetchArray()) 
			{
				echo '<a href="#note'.$i.'">', $row['timestamp'], '</a><br />';
				echo $row['title']?$row['title']:'Không đề', '<br />';
				$i++;
			}
		}
	}
	else
		echo("Couldn't open database!");
?>
	<br />
</p>

<p align="left">
	<a href="addnote.wml">Tạo 1 ghi chú mới:</a><br />
	<a href="">Về trang chủ</a>
<br/> <br/>
	&#169;2011 -  Vutrankien - vukien@hcm.fpt.vn<br/>
</p>

</card>
<?php
	if($results)
	{
		//duyệt các phần tử mảng trả về từ csdl
		$i=1;
		while ($row = $results->fetchArray()) 
		{
			# create a new card
			echo '<card id="note',$i,'" title="', $row['title']?$row['title']:'Không đề','">', "\n";
			echo "<p>\n";
			echo $row['content'], "<br />\n";
			echo '<a href="#main">Về nhà</a>', "<br />\n";
			echo '<a href="deletenote.php?noteid='.$row['noteid'].'">Xóa ghi chú này</a><br/>', "\n";
			echo '<anchor><go href="#editnote">
			<setvar name="noteid" value="'.$row['noteid'].'"/>
			<setvar name="title" value="'.$row['title'].'"/>
			<setvar name="content" value="'.$row['content'].'"/></go>Sửa ghi chú này</anchor>', "\n";
			echo "</p>\n";
			echo "</card>\n";
			$i++;
		}
	}
?>
<!-- the last card is a form to edit the note -->
<card id="editnote">
<p>
	<b>Tiêu đề</b><br/>: 
	<input name="title" format="*M" emptyok="True" value="$(title)"/><br />
	<b>Nội dung</b>: 
	<input name="content" format="*M" emptyok="false" value="$(content)"/><br />
	<anchor>
	<go href="editnote.php" method="post">
		<postfield name="mode" value="post"/>
		<postfield name="noteid" value="$(noteid)"/>
		<postfield name="title" value="$(title)"/>
		<postfield name="content" value="$(content)"/>
	</go>
	Lưu
	</anchor>
<br/>
	&#169;2011 -  Vutrankien - vukien@hcm.fpt.vn<br/>  
</p>
</card>
</wml>